package com.yhn.bubblevideo.admin.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yhn.bubblevideo.admin.service.UserService;
import com.yhn.bubblevideo.common.model.BaseResponse;
import com.yhn.bubblevideo.common.model.DeleteBatchRequest;
import com.yhn.bubblevideo.common.model.ResultUtils;
import com.yhn.bubblevideo.model.dto.req.common.DeleteRequest;
import com.yhn.bubblevideo.model.dto.req.user.UserAddRequestDTO;
import com.yhn.bubblevideo.model.dto.req.user.UserEditRequestDTO;
import com.yhn.bubblevideo.model.dto.req.user.UserQueryRequestDTO;
import com.yhn.bubblevideo.model.dto.resp.UserRespDTO;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yhn
 * @version 1.0
 * @description
 * @date 2024/11/24 15:19
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 列表,分页
     */

    @PostMapping("/list")
    @SaCheckLogin
    public BaseResponse<Page<UserRespDTO>> listUserPage(@RequestBody @Valid UserQueryRequestDTO requestParam) {
        Page<UserRespDTO> page = userService.queryPage(requestParam);
        return ResultUtils.success(page);
    }


    /**
     * 保存
     */
    @PostMapping("/add")
    public BaseResponse<Long> saveUser(@RequestBody @Valid UserAddRequestDTO requestParam) {
        Long newId = userService.saveUser(requestParam);
        return ResultUtils.success(newId);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateUser(@RequestBody @Valid UserEditRequestDTO requestParam) {
        boolean result = userService.updateUser(requestParam);
        return ResultUtils.success(result);
    }


    /**
     * 删除
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteUser(@RequestBody DeleteRequest deleteRequest) {
        boolean result = userService.deleteById(deleteRequest);
        return ResultUtils.success(result);
    }

    /**
     * 批量删除
     */
    @PostMapping("/deleteBatch")
    public BaseResponse<Boolean> deleteBatchUsers(@RequestBody DeleteBatchRequest deleteBatchRequest) {
        boolean result = userService.deleteBatchByIds(deleteBatchRequest.getIds());
        return ResultUtils.success(result);
    }
}
